import java.util.*;
import java.util.stream.Collectors;

public class Leetcode824 {

    public static void main(String[] args) {

    }
    private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');

    public String toGoatLatin(String sentence) {
        StringBuilder res = new StringBuilder();
        int length = sentence.length();

        char[] chars = sentence.toCharArray();
        StringBuilder a = new StringBuilder("a");

        int start;
        boolean s;
        for (int index = 0; index < length; ++index) {
            start = index;
            if (!VOWELS.contains(chars[index])) {
                ++start;
                s = true;
            } else {
                s = false;
            }

            while (start < length && chars[start] != ' ') {
                res.append(chars[start]);
                ++start;
            }

            if (s) {
                res.append(chars[index]);
            }

            res.append("ma").append(a).append(" ");
            index = start;

            a.append("a");
        }

        return res.deleteCharAt(res.length() - 1).toString();
    }
}
